package com.zyzd.baseprojectofvolly.utils.volley;

import android.os.AsyncTask;

import com.android.volley.DefaultRetryPolicy;
import com.android.volley.Response;
import com.zyzd.commontools.image.ImageUtil;

import java.io.File;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by moon.zhong on 2015/3/2.
 */
public class UploadApi {

    /**
     * 上传图片文件必要参数
     */
    public static final String FILE_PART_NAME = "uploadimg";

    /**
     * 上传单张图片（推荐）
     *
     * @param file
     * @param listener
     * @param errorListener
     * @param params
     * @param tag
     */
    public static void upLoadImg(File file, Response.Listener listener, Response.ErrorListener errorListener, Map<String, String> params, Object tag) {
        upLoadImg(file.getAbsoluteFile(), listener, errorListener, params, tag);
    }

    /**
     * 上传单张图片（推荐）
     *
     * @param filePath
     * @param listener
     * @param errorListener
     * @param params
     * @param tag
     */
    public static void upLoadImg(final String filePath, final Response.Listener listener, final Response.ErrorListener errorListener, final Map<String, String> params, final Object tag) {
        new AsyncTask<Object, Void, File>() {
            @Override
            protected void onPreExecute() {
            }

            @Override
            protected File doInBackground(Object... params) {
                return ImageUtil.imageZoom(filePath, 100);
            }

            @Override
            protected void onPostExecute(File file) {
                MultipartRequest multipartRequest = new MultipartRequest(getUploadImageUrl(), listener, errorListener, FILE_PART_NAME, file, params);
                multipartRequest.setRetryPolicy(new DefaultRetryPolicy(10000,
                        DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
                VolleyHelper.addRequest(multipartRequest, tag);
            }
        }.execute();


    }

    /**
     * 上传多张图片（推荐）
     *
     * @param files
     * @param listener
     * @param errorListener
     * @param params
     * @param tag
     */
    public static void upLoadImg(final List<File> files, final Response.Listener listener, final Response.ErrorListener errorListener, final Map<String, String> params, final Object tag) {

        new AsyncTask<Object, Void, List<File>>() {
            @Override
            protected void onPreExecute() {
            }

            @Override
            protected List<File> doInBackground(Object... params) {
                ArrayList<File> zoomFiles = new ArrayList<>();
                if (files != null && files.size() > 0) {
                    for (File file : files) {
                        File zoomFile = ImageUtil.imageZoom(file.getAbsolutePath(), 100);
                        if (zoomFile != null) {
                            zoomFiles.add(zoomFile);
                        }
                    }
                }
                return files;
            }

            @Override
            protected void onPostExecute(List<File> zoomFiles) {
                MultipartRequest multipartRequest = new MultipartRequest(getUploadImageUrl(), listener, errorListener, FILE_PART_NAME, zoomFiles, params);
                multipartRequest.setRetryPolicy(new DefaultRetryPolicy(10000,
                        DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                        DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
                VolleyHelper.addRequest(multipartRequest, tag);
            }
        }.execute();
    }

    /**
     * TODO 设置上传图片的路径
     *
     * @return
     */
    public static String getUploadImageUrl() {
        return "";
    }
}
